const { spawn } = require('child_process');
require('dotenv').config();

const entry = process.env.APP_ENTRY || 'index.js';
const child = spawn('node', [entry], { stdio: ['inherit', 'pipe', 'pipe'] });

child.stdout.on('data', (data) => {
  process.stdout.write(data);
});

child.stderr.on('data', (data) => {
  process.stderr.write(data);
});
child.on('error', (err) => {
  console.error('❌ 启动失败:', err);
});

child.on('spawn', () => {
  console.log('✅ 子进程已启动，程序正在运行...');
});

child.on('exit', (code) => {
  if (code === 0) {
    console.log('✅ 程序正常退出');
  } else {
    console.error('❌ 程序异常退出，退出码:', code);
  }
});
